package main

import (
	"fmt"
	"time"
	"sync"
)

var wg sync.WaitGroup

func main() {
	var intChan chan int = make(chan int, 5)

	wg.Add(2)
	go write(intChan)
	go read(intChan)

	wg.Wait()

}

// 读取数据
func read(intChan chan int) {
	defer wg.Done()

	for v := range intChan {
		fmt.Println("读取的数据为: ", v)
		time.Sleep(time.Second)
	}
}

// 写入数据
func write(intChan chan int) {
	defer close(intChan)
	defer wg.Done()

	for i := 0; i < 5; i++ {
		intChan <- i
		fmt.Println("写入的数据为: ", i)
		time.Sleep(time.Second)
	}
}